﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

public partial class Main_Inventory_ItemGroup : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    string username;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (Request.QueryString["action"] != null)
        {
            if (Request.QueryString["action"].ToString() == "addnew")
            {
                PnlAddNew.Visible = true;
                if (!IsPostBack)
                {
                    bindddlgname();
                }
            }
            else if (Request.QueryString["action"].ToString() == "view")
            {
                PnlView.Visible = true;
                if (!IsPostBack)
                {
                    bindgriddetel();
                }
            }
        }

    }
    public void bindddlgname() 
    {
        DataTable dtddlgname = DU.getdata("select igrname,igrid from ivr_itemgroup");
        ddlgroup.DataSource = dtddlgname;
        ddlgroup.DataTextField = "igrname";
        ddlgroup.DataValueField = "igrid";
        ddlgroup.DataBind();
        ddlgroup.Items.Insert(0, new ListItem("--None--", "0")); 
    }
    public void bindgriddetel()
    {
        DataTable dtdetails = DU.getdatausingprocedure("USP_IVR_GetAllItemGroups");
        griddetils.DataSource = dtdetails;
        griddetils.DataBind();
        custPager.TotalPages = 20; 
    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        //DataTable dtpid=DU.getdata("select parent_igrid from ivr_itemgroup where igrid='"+ddlgroup.SelectedValue+"' and igrname='"+ddlgroup.SelectedItem.Text+"'");
        // int parentid=dtpid.Rows[0]["parent_igrid"].ToString();

        //lblMsg.Text = ddlgroup.SelectedValue + "  name  " + ddlgroup.SelectedItem.Text;

        DataTable dtrete = DU.getdata("select igrname from ivr_itemgroup where igrname='"+txtigname.Text+"'");
         if (dtrete.Rows.Count > 0)
         {
             //RequiredFieldValidator1.Visible = true;
            // RequiredFieldValidator1.Text = "Item Name is already Exists";
             lblMsg.Attributes.Remove("class");
             lblMsg.Attributes.Add("class", "false");
             lblMsg.Text = "ItemGroupName is already Exits...";
            
         }
         else
         {
             SqlParameter[] parm1 = { 
                               new SqlParameter("@iGRID","0"),
                               new SqlParameter("@iGRNAME",txtigname.Text),
                               new SqlParameter("@iGRALTNAME",txtaname.Text),
                               new SqlParameter("@parentGRID",ddlgroup.SelectedValue),
                               new SqlParameter("@cname",Session["username"].ToString()),
                               };
             int ins = DU.executeprocedure("USP_IVR_InsertItemGroup", parm1);
             if (ins > 0)
             {
                 bindgriddetel();
                 bindddlgname();
                 txtaname.Text = "";
                 txtigname.Text = "";
                 txtigname.Focus();
                 lblMsg.Attributes.Remove("class");
                 lblMsg.Attributes.Add("class", "true");
                 lblMsg.Text = "Item Group Created...";
                 txtigname.Focus();
             }
             else
             {

             }
         }

    }
    protected void griddetils_RowEditing(object sender, GridViewEditEventArgs e)
    {
        griddetils.EditIndex = e.NewEditIndex;
        bindgriddetel();
    }
    protected void griddetils_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        griddetils.EditIndex = -1;
        bindgriddetel();
    }
    protected void griddetils_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string id = griddetils.DataKeys[e.RowIndex].Value.ToString();
        string Itemgroupname = ((TextBox)griddetils.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        string alternatenaem = ((TextBox)griddetils.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        string parentid = ((DropDownList)griddetils.Rows[e.RowIndex].Cells[3].Controls[1]).SelectedValue;
        string parentgroupname = ((DropDownList)griddetils.Rows[e.RowIndex].Cells[3].Controls[1]).SelectedItem.Text;
        //lblMsg.Text = Itemgroupname + "  " + alternatenaem + "  " + id + "   " + parentgroupname + "   " + parentid + "  ";// + hf1.Value.ToString();
        //string parentid = hf1.Value.ToString();
        TextBox t1 = (TextBox)griddetils.Rows[e.RowIndex].Cells[1].Controls[0];

        if (t1.Text != string.Empty)
        {
            DataTable dtret = DU.getdata("select igrname from ivr_itemgroup where igrname='" + Itemgroupname + "' and igrid!=" + id + "");
            if (dtret.Rows.Count > 0)
            {
                //RequiredFieldValidator1.Visible = true;
                //RequiredFieldValidator1.Text = "Item Name is already Exists";
                lblMsg.Attributes.Remove("class");
                lblMsg.Attributes.Add("class", "false");
                lblMsg.Text = "ItemGroupName is already Exits...";

            }
            else
            {
                SqlParameter[] parmm1 = { 
                                    new SqlParameter("@iGRID",id),
                                    new SqlParameter("@iGRNAME",Itemgroupname),
                                    new SqlParameter("@iGRALTNAME",alternatenaem),
                                    new SqlParameter("@parentGRID",parentid),
                                    new SqlParameter("@ename",Session["Username"])
                                    };
                int succ = DU.executeprocedure("USP_IVR_updateItemGroup", parmm1);
                if (succ > 0)
                {
                    griddetils.EditIndex = -1;
                    bindgriddetel();
                    lblMsg.Attributes.Remove("class");
                    lblMsg.Attributes.Add("class", "true");
                    lblMsg.Text = "Updated...";
                }
            }

        }
        else
        {
            lblMsg.Attributes.Remove("class");
            lblMsg.Attributes.Add("class", "false");
            lblMsg.Text = "Please Enter Item Group Name";
        }
    }
    protected void griddetils_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void griddetils_RowDataBound(object sender, GridViewRowEventArgs e)  
    {
        //string id;
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{

        //}
        

        if (e.Row.RowIndex != griddetils.EditIndex)
        {

            if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState != DataControlRowState.Edit)
            {
                string id = griddetils.DataKeys[e.Row.RowIndex].Value.ToString();

                DataTable dtgid = DU.getdata("select IGRID from ivr_itemgroup where IGRID=" + id + "");
                //lblMsg.Text = dtgid.Rows[0]["igrid"].ToString();
                if (dtgid.Rows[0]["igrid"].ToString() == "1")
                {
                    e.Row.Cells[3].Controls[0].Visible = false;
                }
                else
                {
                    e.Row.Cells[3].Controls[0].Visible = true;
                }

            }
        }
        else if (e.Row.RowIndex == griddetils.EditIndex && e.Row.RowIndex != -1)
        {
            //DropDownList ddl = (DropDownList)e.Row.FindControl("ddlpname");
            DropDownList ddl = (DropDownList)e.Row.FindControl("ddlpname");
            HiddenField hf = (HiddenField)e.Row.FindControl("HFPrimary");
            HiddenField hf1 = (HiddenField)e.Row.FindControl("HiddenField1");
            DataTable dt1 = DU.getdata("select igrname,igrid from ivr_itemgroup");

            ddl.DataSource = dt1;
            ddl.DataTextField = "igrname";
            ddl.DataValueField = "igrid";
            ddl.DataBind();
            ddl.Items.Insert(0, new ListItem("None", " "));
            if (hf.Value != string.Empty)
            {
                ddl.SelectedValue = hf.Value;
            }
           

            //lblMsg.Text = Itemgroupname + "  " + alternatenaem + "  " + id + "   " + parentgroupname + "   " + parentid + "  " + hf1.Value.ToString();

         
        }
    }
    protected void griddetils_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        griddetils.PageIndex = e.NewPageIndex;
        bindgriddetel();
    }

    protected void Close_Click(object sender, EventArgs e)
    {
        Response.Redirect("ItemView.aspx");
    }

    protected void custPager_PageChanged(object sender, CustomPageChangeArgs e)
    {
        griddetils.PageSize = e.CurrentPageSize;
        griddetils.PageIndex = e.CurrentPageNumber;
        bindgriddetel();
    }
    protected void griddetils_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.Pager)
        {
            if (e.Row.RowState == DataControlRowState.Alternate)
            {
                e.Row.Attributes.Add("onmouseover", "this.className='rowhover'");
                e.Row.Attributes.Add("onmouseout", "this.className='gridview_alternate'");
                //e.Row.Attributes.Add("onclick", "this.className='rowselected'");
            }
            else if (e.Row.RowState == DataControlRowState.Normal)
            {
                e.Row.Attributes.Add("onmouseover", "this.className='rowhover'");
                e.Row.Attributes.Add("onmouseout", "this.className='gridviewitem'");
                //e.Row.Attributes.Add("onclick", "this.className='rowselected'");
            }
        }
    }
}
